/*
 * Copyright (C) 2016 huanghaibin_dev <huanghaibin_dev@163.com>
 * WebSite https://github.com/MiracleTimes-Dev
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.calender.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import androidx.core.content.ContextCompat;

import com.example.calender.R;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.YearView;


/**
 * 自定义年视图
 * Created by huanghaibin on 2018/10/9.
 */
@SuppressWarnings("unused")
public class CustomYearView extends YearView {

    private int mTextPadding;
    /**
     * 闰年字体
     */
    private final Paint mLeapYearTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint monthTextPaint;
    private final Paint dayTextPaint;
    private Paint weekTextPaint;

    public CustomYearView(Context context) {
        super(context);
        mTextPadding = dipToPx(context, 3);

        mLeapYearTextPaint.setTextSize(dipToPx(context, 12));
        mLeapYearTextPaint.setColor(0xffd1d1d1);
        mLeapYearTextPaint.setAntiAlias(true);
        mLeapYearTextPaint.setFakeBoldText(true);

        monthTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        monthTextPaint.setTextSize(dipToPx(context,12));
        monthTextPaint.setColor(ContextCompat.getColor(context, R.color.text_second_666666));
        monthTextPaint.setAntiAlias(true);

        //mWeekTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //mWeekTextPaint.setTextSize(dipToPx(context,7));
        //mWeekTextPaint.setColor(ContextCompat.getColor(context, R.color.text_third_999999));
        //mWeekTextPaint.setAntiAlias(true);
        mWeekTextPaint.setFakeBoldText(false);
        mCurMonthTextPaint.setFakeBoldText(false);

        dayTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        dayTextPaint.setTextSize(dipToPx(context,10));
        dayTextPaint.setColor(ContextCompat.getColor(context, R.color.text_black_333333));
        dayTextPaint.setAntiAlias(true);
        mSelectedPaint.setColor(Color.parseColor("#4D2490FF"));
    }

    @Override
    protected void onDrawMonth(Canvas canvas, int year, int month, int x, int y, int width, int height) {

        String text = getContext()
                .getResources()
                .getStringArray(com.haibin.calendarview.R.array.month_string_array)[month - 1];
        canvas.drawText(text,
                x + (width / 2) - getTextWidth(monthTextPaint,text) /2,
                y + mMonthTextBaseLine,
                monthTextPaint);
        if (month == 2 && isLeapYear(year)) {
            float w = getTextWidth(mMonthTextPaint, text);

            canvas.drawText("闰年",
                    x + (width / 2)  + w + dipToPx(getContext(), 6),
                    y + mMonthTextBaseLine,
                    mLeapYearTextPaint);
        }
        Paint linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#F7F7F7"));
        canvas.drawLine(x,y + height,x + width,y + height,linePaint);
    }

    private float getTextWidth(Paint paint, String text) {
        return paint.measureText(text);
    }

    /**
     * 是否是闰年
     *
     * @param year year
     * @return 是否是闰年
     */
    private static boolean isLeapYear(int year) {
        return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }



    @Override
    protected void onDrawWeek(Canvas canvas, int week, int x, int y, int width, int height) {
        String text = getContext().getResources().getStringArray(R.array.chinese_week_string_array)[week];
        canvas.drawText(text, x + width / 2, y + mWeekTextBaseLine, mWeekTextPaint);
    }


    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        canvas.drawRoundRect(new RectF(x,y + 15,x + mItemWidth,y + mItemHeight - 15),10,10,mSelectedPaint);
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {

    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;

        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, hasScheme ? mSchemeTextPaint : mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, calendar.isCurrentDay() ? mCurDayTextPaint : mSchemeTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, calendar.isCurrentDay() ? mCurDayTextPaint : mCurMonthTextPaint);
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
